package cn.jorian.framework.service.impl;

import cn.jorian.framework.bean.DataBean;
import cn.jorian.framework.bean.InboundBackBean;
import cn.jorian.framework.bean.ResponseResult;
import cn.jorian.framework.net.NetManager;
import cn.jorian.framework.service.IIndocBackService;
import cn.jorian.framework.util.GsonUtil;
import cn.jorian.framework.util.ParamUtil;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.io.IOException;
import java.util.UUID;

@Service
public class IndocBackServiceImpl implements IIndocBackService {

    @Override
    public ResponseResult indocBack() {

        InboundBackBean inboundBackBean = new InboundBackBean();
        inboundBackBean.setOwnerCode("19940");
        inboundBackBean.setOrderNo("540530");
        inboundBackBean.setBusinessType("1");
        inboundBackBean.setOrderTime("2023/02/07");
        inboundBackBean.setOmsOrderNo("12345");


        InboundBackBean.DtlBean dtlBean = new InboundBackBean.DtlBean();
        dtlBean.setOrderDtlNo("12458890");
        dtlBean.setOrderStockDtlNo("121212");
        dtlBean.setOwnerOgoodsCode("227929");
        dtlBean.setCommonName("葫芦岛UAT测试品（地采）");
        dtlBean.setBatchNbr("20240911");
        dtlBean.setPrdDate("2024-09-11");
        dtlBean.setValidDate("2024-09-21");
        dtlBean.setQty(5.0);
        dtlBean.setGoodsStatus("-1");
//        dtlBean.setBusinessType(inboundBackBean.getBusinessType());
//        dtlBean.setOrderNo(inboundBackBean.getOrderNo());
//        dtlBean.setOmsOrderNo(inboundBackBean.getOmsOrderNo());

        inboundBackBean.addList(dtlBean);


        String json = "{\"record\":{\"recordDtl\":{\"omsOrderNo\":\"109HKDJ250217000002\",\"adjustType\":\"1\",\"ownerCode\":\"39725\",\"adjustTime\":\"2025-02-19 16:15:01\",\"adjustmentNoWms\":\"109HKDJ250217000002\",\"applicationNo\":null,\"asnNo\":null,\"remarks\":null,\"dtlList\":[{\"adjustDtlNo\":\"1\",\"applicationNoDtl\":null,\"omsOrderDtlNo\":\"109HKDJ250217000001-1\",\"ownerOgoodsCode\":\"223600\",\"cbCode\":\"83805\",\"qty\":1,\"adjustReason\":\"4\",\"custCode\":null,\"prdBatchOld\":null,\"prdBatchNew\":null,\"batchNbrOld\":\"240445\",\"batchNbrNew\":\"240445\",\"goodsStatusOld\":\"3\",\"goodsStatusNew\":\"1\",\"prdDateOld\":\"2024-04-25\",\"prdDateNew\":\"2024-04-25\",\"validDateOld\":\"2027-02-31\",\"validDateNew\":\"2027-02-31\",\"approvalNumberOld\":null,\"approvalNumberNew\":null,\"registrationNumberOld\":null,\"registrationNumberNew\":null,\"srlNbrList\":null}]}},\"interfaceType\":\"Adjustment\",\"channelType\":\"NMCF-ERP\",\"trackNo\":\"109HKDJ250217000002\",\"interfaceNbr\":\"e322e1d7-29aa-42ce-9b12-e3a95dd685c1\"}";

        //data
        DataBean<InboundBackBean> dataBean = new DataBean<>();
        dataBean.setInterfaceType("Inbound_Back");
        dataBean.setChannelType("NMCF-ERP");
        dataBean.setInterfaceNbr(UUID.randomUUID().toString());
        dataBean.addRecord(inboundBackBean);

        DataBean<InboundBackBean> dataBean1 = GsonUtil.fromJson(json, new TypeToken<DataBean<InboundBackBean>>(){});

//        try {
//            //同步post请求
//            Response<ResponseResult> response = NetManager.newInstance().getServiceApi().indocBack(ParamUtil.getParam(dataBean1)).execute();
//            ResponseResult result = response.body();
//            return result;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return ResponseResult.fail(e.getMessage());
//        }

        NetManager.newInstance().getServiceApi().indocBack(ParamUtil.getParam(dataBean1)).enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult result = response.body();
                System.out.println(GsonUtil.toJson(result));
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable throwable) {
                throwable.printStackTrace();
            }
        });

        return ResponseResult.success();
    }
}
